<?php
/**
 * coreSessionMemcache
 *
 * Memcache를 이용하여 세션데이터를 저장하고 관리합니다.
 * PHP Memcache Extension 을 필요로 합니다.
 *
 * @package core
 * @author  Platform Team
 */

class coreSessionMemcache extends coreSessionAbstract
{
    /**
     * Memcache 객체
     * @var Memcache
     */
    private $oMemcache;

    /**
     * 초기화
     */
    protected function initialize()
    {
        $this->oMemcache = new Memcache();
    }

    /**
     * open Handler
     */
    public function open($sSavePath, $sSessName)
    {
        if ($this->sessMemcachePersistent === true) {
            $isConnect = $this->oMemcache->pconnect($this->sessMemcacheHost, $this->sessMemcachePort);
        } else {
            $isConnect = $this->oMemcache->connect($this->sessMemcacheHost, $this->sessMemcachePort);
        }

        if ($isConnect === false) {
            throw new Exception('can not connect to memcache host - ' . $this->sessMemcacheHost . ':' . $this->sessMemcachePort);
        }
    }

    /**
     * close Handler
     */
    public function close()
    {
        return $this->oMemcache->close();
    }

    /**
     * read Handler
     */
    public function read($sSessId)
    {
        $sData = $this->oMemcache->get($sSessId);

        return $this->loadData($sData);
    }

    /**
     * write Handler
     */
    public function write($sSessId, $sData)
    {
        if ($this->getPreventSave()) {
            return;
        }

        $sData = $this->saveData($sData);

        return $this->oMemcache->set($sSessId, $sData, false, $this->sessExpire);
    }

    /**
     * destroy Handler
     */
    public function destroy($sSessId)
    {
        return $this->oMemcache->delete($sSessId);
    }

    /**
     * garbage collector Handler
     */
    public function gc($iMaxLife)
    {
        //Empty
    }
}
